﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Portal;
using Microsoft.SharePoint.WebControls;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

namespace KoPages.CONTROLTEMPLATES.userControls
{
    public partial class _Test : UserControl
    {

        List<dsData> dsProperty = new List<dsData>();

        protected void Page_Load(object sender, EventArgs e)
        {
            TheTest("http://localhost/");
        }

        private void TheTest(string sURL)
        {
            using (SPSite site = new SPSite(sURL))
            {
                using (SPWeb spUWeb = site.OpenWeb())
                {

                    string OriginalUrl = site.Url;

                    //spUWeb.Users.q
                    SPServiceContext context = SPServiceContext.GetContext(site);
                    UserProfileManager uprofileManager = new UserProfileManager(context);
                    // ProfileSearchManager profileSM = uprofileManager.;
                    // IEnumerator profileEnumerator = uprofileManager.GetEnumerator();

                    foreach (UserProfile uPr in uprofileManager)
                    {
                        if (uPr["SPS-Birthday"].Value != null)
                        {
                            DateTime dtBirthday, dtHireDate;
                            dsData dRow = new dsData();

                            if (uPr["SPS-Birthday"].Value != null)
                            {
                                dtBirthday = DateTime.Parse(uPr["SPS-Birthday"].Value.ToString());
                                dRow.Birthday = dtBirthday;
                            }

                            if (uPr["SPS-HireDate"].Value != null)
                            {
                                dtHireDate = DateTime.Parse(uPr["SPS-HireDate"].Value.ToString());
                                dRow.HireDate = dtHireDate;
                            }

                            if (uPr["PreferredName"].Value != null)
                                dRow.FullName = uPr["PreferredName"].Value.ToString();
                            if (uPr["SPS-JobTitle"].Value != null)
                                dRow.Staff = uPr["SPS-JobTitle"].Value.ToString();
                            if (uPr["Department"].Value != null)
                                dRow.Department = uPr["Department"].Value.ToString();
                            if (uPr["Office"].Value != null)
                                dRow.Office = uPr["Office"].Value.ToString();
                            if (uPr["PictureUrl"].Value != null)
                                dRow.ImageUrl = uPr["PictureUrl"].Value.ToString();
                            else
                                dRow.ImageUrl = sURL + "/_layouts/IMAGES/no-photo.png";
                            if (uPr.PersonalSite != null)
                                dRow.SiteUrl = uPr.PersonalSite.Url.ToString();
                            //--
                            dsProperty.Add(dRow);
                        }
                    }

                    GridView1.DataSource = dsProperty;
                    GridView1.DataBind();
                }
            }
        }
    }


    public class dsData
    {
        private int _id;
        public int ID { get { return _id; } set { _id = value; } }
        private string _FirstName = "";
        public string FirstName { get { return _FirstName; } set { _FirstName = value; } }
        private string _LastName = "";
        public string LastName { get { return _LastName; } set { _LastName = value; } }
        private string _Department = "";
        public string Department { get { return _Department; } set { _Department = value; } }
        private string _Office = "";
        public string Office { get { return _Office; } set { _Office = value; } }
        private string _ImageUrl = "";
        public string ImageUrl { get { return _ImageUrl; } set { _ImageUrl = value; } }
        private string _SiteUrl = "";
        public string SiteUrl { get { return _SiteUrl; } set { _SiteUrl = value; } }
        private string _Phone = "";
        public string Phone { get { return _Phone; } set { _Phone = value; } }
        private string _Mail = "";
        public string Mail { get { return _Mail; } set { _Mail = value; } }
        private string _Staff = "";
        public string Staff { get { return _Staff; } set { _Staff = value; } }
        private DateTime _Birthday;
        public DateTime Birthday { get { return _Birthday; } set { _Birthday = value; } }
        private DateTime _HireDate;
        public DateTime HireDate { get { return _HireDate; } set { _HireDate = value; } }
        private string _FullName = "";
        public string FullName { get { return _FullName; } set { _FullName = value; } }
    }

}

